package stardust.core.network.protocol.http.support;


import java.util.HashMap;
import java.util.Map;


public class URL
{

  public static final Map<String, Integer> DEFAULT_PROTOCOL_PORT = new HashMap<String, Integer>();

  static
  {
    URL.DEFAULT_PROTOCOL_PORT.put("http", 80);
    URL.DEFAULT_PROTOCOL_PORT.put("https", 80);
    URL.DEFAULT_PROTOCOL_PORT.put("ftp", 21);
  }

  private String url;

  private String protocol;

  private String host;

  private Integer port;

  private URI uri;

  public URL(String url)
  {
    this.url = url.replace('\\', '/');

    int offset;

    /*
     * 提取协议。
     */
    offset = this.url.indexOf("://");
    if (offset == -1)
    {
      throw new IllegalArgumentException("无法从URL[" + this.url + "]提取协议类型。");
    }
    else
    {
      this.protocol = this.url.substring(0, offset);
    }

    /*
     * 提取主机和端口。
     */
    if (offset == -1)
    {
      throw new IllegalArgumentException("无法从URL[" + this.url + "]提取主机地址。");
    }
    else
    {
      int portOffset = this.url.indexOf(":", offset + "://".length());
      
      try
      {
      if (portOffset != -1 && this.url.substring(offset + "://".length(), this.url.indexOf("/", portOffset + 1)).indexOf('/') != -1)
      {
    	  portOffset = -1;
      }
      }
      catch (Exception e)
      {
    	  
      }

      if (portOffset == -1)
      {
        if (this.url.indexOf("/", offset + "://".length()) == -1)
        {
          this.host = this.url.substring(offset + "://".length());
          offset = this.url.length();
        }
        else
        {
          this.host = this.url.substring(offset + "://".length(), this.url.indexOf("/", offset + "://".length()));
          offset = this.url.indexOf("/", offset + "://".length());
        }

        // 默认端口。
        this.port = URL.DEFAULT_PROTOCOL_PORT.get(this.protocol.toLowerCase());
      }
      else
      {
        this.host = this.url.substring(offset + "://".length(), portOffset);

        if (this.url.indexOf("/", portOffset + 1) == -1)
        {
          this.port = Integer.parseInt(this.url.substring(portOffset + 1));
          offset = this.url.length();
        }
        else
        {
          this.port = Integer.parseInt(this.url.substring(portOffset + 1, this.url.indexOf("/", portOffset + 1)));
          offset = this.url.indexOf("/", portOffset + 1);
        }
      }
    }

    /*
     * 提取路径。
     */
    this.uri = new URI(this.url.substring(offset));
  }

  public String getOriginalString()
  {
    return this.url;
  }

  public String getProtocol()
  {
    return this.protocol;
  }

  public String getHost()
  {
    return this.host;
  }

  public Integer getPort()
  {
    return this.port;
  }

  public URI getURI()
  {
    return this.uri;
  }
  
  public static void main(String[] args)
  {
	  //new URL("http://www.gamersky.com/showimage/id_gamersky_01.shtml?http://img1.gamersky.com/image2011/10/20111001h_01/01.jpg");
	  new URL("http://www.gamersky.com/news/201203/192797.shtml");
  }

}
